<?php 
	namespace Admin\Controller;

	Class AddbookController extends AdminController
	{
		// 搜索主页
		// public function index()
		// {	
		// 	$this->display();
		// }

		public function index()
		{
			$showapi_appid = '27066'; 
		    $showapi_secret = '9e0f60470f5b4078bf41b118e7da7a7c'; 
		    $paramArr = array(
		         'showapi_appid'=> $showapi_appid,
		         'keyword'=> I('keyword',null),
		         'typeId'=> I('type',null),
		         'pre_match'=> "",
		         'page'=> I('id',1)
		       
		         //添加其他参数
		    	);

		    $keyword = I('keyword',null);
			// echo "<pre>";
		 	// var_dump($keyword);
		    $page = I('id',1);


		    $param = $this->createParam($paramArr,$showapi_secret);
		    $url = 'http://route.showapi.com/211-2?'.$param; 
		    // 获取小说内容
		    $result = file_get_contents($url);
		    // 对内容进行解码解码
		    $jsonObj = json_decode($result);

		    $book = $jsonObj->showapi_res_body->pagebean->contentlist;
		   	$aa = array();
		    foreach($book as $vo){
		    	$a = explode(" ",$vo->name);
				$aa[] = array('name'=>$a['0'],'id'=>$vo->id,'author'=>$vo->author);

		    }
		    // echo "<pre>";
		   	// var_dump($jsonObj);

			$this->assign('book',$aa);
			$this->assign('keyword',$keyword);
			$this->assign('page',$page);
			
			$this->display();
		}

		 //创建参数(包括签名的处理)
	    public function createParam ($paramArr,$showapi_secret) {
	         $paraStr = "";
	         $signStr = "";
	         ksort($paramArr);
	         foreach ($paramArr as $key => $val) {
	             if ($key != '' && $val != '') {
	                 $signStr .= $key.$val;
	                 $paraStr .= $key.'='.urlencode($val).'&';
	             }
	         }
	         $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
	         $sign = strtolower(md5($signStr));
	         $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验

	         return $paraStr;
	    }

	    public function add()
	    {
	    	$showapi_appid = '27066'; 
		    $showapi_secret = '9e0f60470f5b4078bf41b118e7da7a7c'; 
		    $paramArr = array(
		         'showapi_appid'=> $showapi_appid,
		         'bookId'=> I('bookid'),
		         //添加其他参数
		    	);


		    $param = $this->createParam($paramArr,$showapi_secret);
		    $url = 'http://route.showapi.com/211-1?'.$param; 
		    // 获取小说内容
		    $result = file_get_contents($url);
		    // 对内容进行解码解码
		    $result = json_decode($result);

		    $book = $result->showapi_res_body->book;


		    $typeName = $book->typeName;
		    $author = $book->author;
		    $name =explode(' ',$book->name)['0'];

		    // 小说的类型
		    $this->assign('typeName',$typeName);
		    // 小说的作者
		    $this->assign('author',$author);
		    // 小说的书名
		    $this->assign('name',$name);
		    // 小说的ID
		    $this->assign('bookid',I('bookid'));

		    // var_dump($author);

		    $this->display();
	    }

	    public function doadd()
		{	
			$a = I();

			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =      ''; // 设置附件上传目录
			// 上传文件 
			$info   =   $upload->upload();
			if(!$info) {// 上传错误提示错误信息  
				
				$this->error($upload->getError());
			}
			else{// 上传成功 获取上传文件信息    
				foreach($info as $file){        
					$a['image']=$file['savepath'].$file['savename'];
				}
			}
			
			if(M('book')->add($a) >= 0){
				$this->success("添加成功！",U('Addbook/index'));
			}else{
				$this->error("添加失败");
			}
		}


		public function total()
		{
			$book = M('book')->select();

			$this->assign('book',$book);

			$this->display();
		}

		public function delajax()
		{
			// I();
			// var_dump(I());
			// exit;
			$book = M('book')->where(I())->delete();
			$mbook = M('mbook')->where(I())->delete();

			$this->ajaxreturn($book);
		}

		// edit查询
		public function edit()
		{
			$link = M('book');

			// var_dump(I('id'));

			$array = $link->where(I())->find();

			// var_dump($array);
			
			$this->assign('vo',$array);
			$this->display();
		} 

		// 执行edit修改
		public function doedit()
		{
			$link = M('book');

			$a = I();

			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =      ''; // 设置附件上传目录
			// 上传文件 
			$info   =   $upload->upload();
			if(!$info) {// 上传错误提示错误信息  
				
				$this->error($upload->getError());
			}
			else{// 上传成功 获取上传文件信息    
				foreach($info as $file){        
					$a['image']=$file['savepath'].$file['savename'];
				}
			}

			// var_dump($a);
			// exit;
			if($link->save($a)>0){
				$this->success("修改成功！",U('Addbook/total'));
			}else{
				$this->error("修改失败");
			}
		}

	}
